package kr.co.m2soft.figure
{
	import flash.display.Graphics;

	public class Arrow
	{
		
		public static const ARROW_LINE:int = 0;
		public static const ARROW_FILL:int = 1;
		public static const ARROW_LINE_COLOR_FILL:int = 2;
		
		private var _x1:Number;
		private var _x2:Number;
		private var _y1:Number;
		private var _y2:Number;
		private var _color:uint = 0x000000;
		private var _thickness:Number = 2;
		private var _target:Graphics;
		private var _filColor:uint = 0xFFFFFF;
		private var _type:int = 2;
		private var _isShowLine:Boolean = false;
		
		public function Arrow()
		{
			
		}
		
		public function draw():void
		{
			var angle:Number = Math.atan2( y1-y2, x1-x2 );  //화살표의 회전각도
			var headSize:Number = this.thickness*5; //화살표의 헤더부분 길이
			var Degree2Radian:Number = Math.PI/180; //Degree -> Radian을 바꿔줄때 이 값을 곱함
			var cos1:Number = Math.cos( Degree2Radian * 45 );   //화살표 촉의 x축값은  cos(45도 ) 임
			var sin1:Number = Math.sin( Degree2Radian * 45 );   //화살표 촉의 y축값은 sin(45도) 임 
			var cos2:Number = Math.cos( angle ); //화살표 회전에 대한 cos 값
			var sin2:Number = Math.sin( angle ); //화살표 회전에 대한 sin 값
			var length:Number = Math.sqrt( Math.abs(x1-x2)*Math.abs(x1-x2) + Math.abs(y1-y2)*Math.abs(y1-y2));  //화살표 길이
			
			var dx:Number = length - headSize * cos1;   //화살표 촉 양 끝단 x축 값
			var dy1:Number = headSize * sin1;         //화살표 촉 양 끝단 y축 값 1
			var dy2:Number = -headSize * sin1;      //화살표 촉 양 끝단 y축 값 2
			
			//회전에 의한 화살표 양 끝단의 위치 (2차 회전행렬 공식 이용 )
			var newX1:Number = x2 + ( dx * cos2 - dy1 * sin2);    
			var newY1:Number = y2 + ( dx * (+sin2) + dy1 * cos2);
			var newX2:Number = x2 + ( dx * cos2 - dy2 * sin2);
			var newY2:Number = y2 + ( dx * (+sin2) + dy2 * cos2);
			
			//계산된 좌표값을 이용해 화살표를 다시 그림
			target.clear();
			target.lineStyle(thickness, color, 1.0 );
			
			// 화살표 축을 그린다.
			if(isShowLine){
				target.moveTo( x1, y1 );
				target.lineTo( x2, y2 );
			}
			target.moveTo( x1, y1 );
			if(type != ARROW_LINE){
				type == ARROW_FILL ? target.beginFill(0xFFFFFF,1) : target.beginFill(color,1);
			}
			
			target.lineTo( newX1, newY1 );
			target.lineTo( x1, y1 );
			target.lineTo( newX2, newY2 );
			
			//화살표를 삼각형으로 만들
			if(type != ARROW_LINE){
				target.lineTo( newX1, newY1 );
				target.endFill();
			}
			
		}
		
		public function get x1():Number
		{
			return _x1;
		}

		public function set x1(value:Number):void
		{
			_x1 = value;
		}

		public function get x2():Number
		{
			return _x2;
		}

		public function set x2(value:Number):void
		{
			_x2 = value;
		}

		public function get y1():Number
		{
			return _y1;
		}

		public function set y1(value:Number):void
		{
			_y1 = value;
		}

		public function get y2():Number
		{
			return _y2;
		}

		public function set y2(value:Number):void
		{
			_y2 = value;
		}

		public function get color():uint
		{
			return _color;
		}

		public function set color(value:uint):void
		{
			_color = value;
		}

		public function get thickness():Number
		{
			return _thickness;
		}

		public function set thickness(value:Number):void
		{
			_thickness = value;
		}

		public function get target():Graphics
		{
			return _target;
		}

		public function set target(value:Graphics):void
		{
			_target = value;
		}

		public function get type():int
		{
			return _type;
		}

		public function set type(value:int):void
		{
			_type = value;
		}

		public function get isShowLine():Boolean
		{
			return _isShowLine;
		}

		public function set isShowLine(value:Boolean):void
		{
			_isShowLine = value;
		}


	}
}